package com.aqielife.aqiemusic.performance.net;

import com.aqielife.aqiemusic.MusicApplication;
import com.aqielife.aqiemusic.performance.timeCounter.Utils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 需要添加到拦截器链
 */
public class NoNetInterceptor implements Interceptor {

    @NotNull
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
        // 用户没有网络强制开启缓存
        if(!Utils.isNetworkConnected(MusicApplication.getInstance())){
            // 添加缓存策略
            builder.cacheControl(CacheControl.FORCE_CACHE);
        }
        // 让拦截器继续运转
        return chain.proceed(builder.build());
    }
}
